<template>
    <div class="app-container">
        <el-card shadow="never">
            <!-- 条件查询 -->
            <el-form v-show="showSearch" ref="queryForm" :model="queryParams" label-width="120px" @keyup.enter.native="submitSearch" @submit.native.prevent>
                <el-row>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="模块分类:" prop="type.value">
                            <el-select v-model="queryParams.type.value" @change="onChangeType">
                                <el-option v-for="(item, index) in type"
                                           :key="index"
                                           :value="item.value" :label="item.label"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="分类名称:" prop="name.value">
                            <el-input v-model="queryParams.name.value" placeholder="请输入分类名称" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item label="状态:" prop="status.value">
                            <el-select v-model="queryParams.status.value" placeholder="请选择状态" clearable>
                                <el-option v-for="(item, index) in status" :key="index" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12" v-if="queryParams.type.value">
                        <el-form-item label="上级分类:" prop="pid.value">
                            <SelectCategory url="/admin/ch.system_category/tree" ref="category"
                                            placeholder="请选择上级分类"
                                            :queryParams="{ type: queryParams.type.value }"
                                            :check-strictly="true"
                                            @change="onChangeCategory"></SelectCategory>
                        </el-form-item>
                    </el-col>
                    <el-col :md="6" :sm="12">
                        <el-form-item class="ele-text-right" label-width="50px">
                            <el-button type="primary" size="mini" @click="submitSearch" icon="el-icon-search" class="ele-btn-icon">搜索 </el-button>
                            <el-button @click="handleResetQuery" size="mini" icon="el-icon-refresh">重置</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>


            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="open" v-hasPermi="api.add">添加</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="api.delete">删除</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" size="mini" v-loading="exportLoading" @click="handleExport" v-hasPermi="api.export">导出</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" @queryTable="initIndex" :columns="columns" printId="#quickadmin"></right-toolbar>
            </el-row>

            <!-- 列表 -->
            <qa-table
                :source="tableData"
                :columns="columns"
                :selection="true"
                :loading="loading"
                @sortChange="sortChange"
                @statusChange="statusChange"
                @selectionChange="selectionChange"
                id="quickadmin"
            >
                <template v-slot:action>
                    <el-table-column label="操作" width="240" align="left">
                        <template slot-scope="scope">
                            <el-button class="btn-mar" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-hasPermi="api.edit">编辑</el-button>
                            <el-button class="btn-mar" size="mini" type="text" icon="el-icon-tickets" @click="handleDetail(scope.row)" v-hasPermi="api.find">详情</el-button>
                            <el-button class="btn-mar" size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="api.delete">删除</el-button>
                        </template>
                    </el-table-column>
                </template>
            </qa-table>
            <!-- 分页 -->
            <pagination :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="initIndex" />
        </el-card>

        <!-- 编辑弹窗 -->
        <el-dialog :title="!disabled ? '编辑' : '详情'" :visible="editOpen"  :before-close="beforeClose" destroy-on-close>
            <EditForm ref="editForm" :findFormData="findFormData" :pageType="queryParams.type.value"
                      :disabled="disabled" @submitForm="submitForm" :editFormParams="editFormParams" @editFormCancel="beforeClose" />
        </el-dialog>
    </div>
</template>

<script>
import { curdMixin } from '@/mixins/curdMixin'
import EditForm from './EditFormCom'
import SelectCategory from "@/components/My/SelectCategory";

export default {
    name: 'Curd',
    mixins: [curdMixin],
    components: { EditForm, SelectCategory },
    data() {
        return {
            // table结构
            columns: [
                {
                    visible: true,
                    label: '编号',
                    prop: 'id',
                    width: 60
                },
                {
                    visible: true,
                    label: '上级分类',
                    prop: 'systemCategory.name',
                },
                {
                    visible: true,
                    label: '类型',
                    prop: 'type',
                    component:'QuickAdminTextColor',
                    formatter: (prop, row) => {
                        let type = ''
                        switch (prop) {
                            case 1:
                                type = 'success'
                                break
                            case 2:
                                type = 'warning'
                                break
                            case 3:
                                type = 'info'
                                break
                            case 4:
                                type = 'primary'
                                break
                            case 5:
                                type = 'primary'
                                break

                        }
                        return {
                            value: prop,
                            type: type,
                            text: row.type_text,
                        }
                    },
                },
                {
                    visible: true,
                    label: '分类名称',
                    prop: 'name',
                },
                {
                    visible: true,
                    label: '分类描述',
                    prop: 'desc',
                },
                {
                    visible: true,
                    label: '排序',
                    prop: 'weigh',
                },
                {
                    visible: true,
                    label: '状态',
                    prop: 'status',
                    component:'QuickAdminTextColor',
                    formatter: (prop, row) => {
                        let type = ''
                        switch (prop) {
                            case 1:
                                type = 'success'
                                break
                            case 2:
                                type = 'warning'
                                break

                        }
                        return {
                            value: prop,
                            type: type,
                            text: row.status_text,
                        }
                    },
                },

            ],
            // 接口地址
            api: {
                index: `/admin/ch.system_category/index`,
                find: `/admin/ch.system_category/find`,
                add: `/admin/ch.system_category/add`,
                edit: `/admin/ch.system_category/edit`,
                delete: `/admin/ch.system_category/delete`,
                export: `/admin/ch.system_category/export`,
                status: `/admin/ch.system_category/status`
            },
            editFormParams: {},
            pageInfo: {
                sort: 'id',
            },
            primaryKey: 'id',
            // 搜索表单是否展开
            searchExpand: false,
            //是否展示搜索
            showSearch: true,
            // 查询参数
            queryParams: {
                type: { value: 1, op: '='},
                name: { value:undefined, op: '%like%'},
                status: { value:undefined, op: '='},
                pid: { value:undefined, op: '='},
            },
            type:[
                { label: "医院类别", value: 1 },
                { label: "科室", value: 2 },
                { label: "医生", value: 3 },
                { label: "交流群", value: 4 },
                { label: "服务内容", value: 5 },
                { label: "医院级别", value: 6 },
            ],
            status:[
                { label: "启用", value: 1 },
                { label: "禁用", value: 2 },
            ],
        }
    },
    created () {
        this.initIndex()
    },
    methods: {
        onChangeType(e) {
            this.queryParams.type.value = undefined
            let timer = setTimeout(()=>{
                this.queryParams.type.value = e
                this.initIndex()
                clearTimeout(timer)
            }, 100)
        },
        onChangeCategory(e, pathLabels) {
            e = e || []
            pathLabels = pathLabels || []
            if (e.length >= 3) {
                this.$set(this.queryParams.pid, 'value', e[2])
            } else if (e.length >= 2) {
                this.$set(this.queryParams.pid, 'value', e[1])
            } else if (e.length >= 1) {
                this.$set(this.queryParams.pid, 'value', e[0])
            } else{
                this.$set(this.queryParams.pid, 'value', undefined)
            }
        },
        handleResetQuery() {
            this.$refs.category.clear()
            this.resetQuery()
        }
    }
}
</script>
<style lang="scss">
.btn-mar {
    margin: 5px !important;
}
</style>
